﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using MockPrjTeam2.Common;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.DAO
{
    public class UserDao
    {
        public UserDao()
        {

        }

        private static User ExtractRawData(DataTable table)
        {
            if (table.Rows.Count > 0)
            {
                DataRow dataReader = table.Rows[0];
                return new User
                           {
                               Account = dataReader["Account"].ToString(),
                               Email = dataReader["Email"].ToString(),
                               Password = dataReader["Password"].ToString(),
                               Role = dataReader["RoleID"].ToString(),
                               Id = Convert.ToInt32(dataReader["UserID"])
                           };
            }
            return null;
        }

        public User GetUser(string userName, string password)
        {
            try
            {
                const string query = "Select * From [User] Where Account = @username And Password = @password1";
                var parameter = new SqlParameter[2];
                parameter[0] = new SqlParameter { ParameterName = "@username", Value = userName };
                parameter[1] = new SqlParameter { ParameterName = "@password1", Value = password };
                var sqlHelper = new SqlHelper();
                using (DataTable reader = sqlHelper.ExecuteSelectQuery(query, parameter))
                {
                    User result = ExtractRawData(reader);
                    return result;
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.StackTrace);
                return null;
            }
        }
    }


}